using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ZombieHigh2.GameState
{
    public class GameStateManager
    {
        private static GameStateManager _manager;

        private Stack<IGameState> gameStack;
        private Game _gameHandle;
        private IGameState _switchState;

        public Game GameHandle
        {
            get { return _gameHandle; }
        }

        public IGameState CurrentState
        {
            get { return gameStack.Peek(); }
        }

        public static GameStateManager Instance
        {
            get { return _manager; }
        }

        private GameStateManager(Game game)
        {
            _gameHandle = game;
            gameStack = new Stack<IGameState>();
        }

        public static void Init(Game game)
        {
            _manager = new GameStateManager(game);
        }

        public void Update(GameTime gameTime)
        {
            foreach (IGameState gameState in gameStack)
            {
                gameState.Update(gameTime);
            }

            if (_switchState != null)
            {
                PopState();
                PushState(_switchState);
                _switchState = null;
            }
        }

        public void Draw(GraphicsDevice device)
        {
            gameStack.Peek().Draw(device);
        }

        public void PopState()
        {
            IGameState gs = gameStack.Pop();
            gs.UnloadGraphics();
        }

        public void PushState(IGameState gameState)
        {
            gameStack.Push(gameState);
            //gameState.LoadGraphics(device);
            gameState.Load();
        }

        public static void SwitchState(IGameState gameState)
        {
            Instance._switchState = gameState;
        }

        public static ContentManager Content
        {
            get { return Instance.gameStack.Peek().Content; }
        }

    }
}
